package Toipc

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/Toipc"
	ToipcReq "github.com/flipped-aurora/gin-vue-admin/server/model/Toipc/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type InterviewService struct {
}

// CreateInterview 创建interview表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewService *InterviewService) CreateInterview(interview *Toipc.Interview) (err error) {
	err = global.GVA_DB.Create(interview).Error
	return err
}

// DeleteInterview 删除interview表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewService *InterviewService) DeleteInterview(interview Toipc.Interview) (err error) {
	err = global.GVA_DB.Delete(&interview).Error
	return err
}

// DeleteInterviewByIds 批量删除interview表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewService *InterviewService) DeleteInterviewByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]Toipc.Interview{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateInterview 更新interview表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewService *InterviewService) UpdateInterview(interview Toipc.Interview) (err error) {
	err = global.GVA_DB.Save(&interview).Error
	return err
}

// GetInterview 根据id获取interview表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewService *InterviewService) GetInterview(id uint) (interview Toipc.Interview, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&interview).Error
	return
}

// GetInterviewInfoList 分页获取interview表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewService *InterviewService) GetInterviewInfoList(info ToipcReq.InterviewSearch) (list []Toipc.Interview, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&Toipc.Interview{})
	var interviews []Toipc.Interview
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&interviews).Error
	return interviews, total, err
}
